#coding=utf-8
import time
import os

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from image.models import Image

class UserProfileManager(models.Manager):
    pass

class UserProfile(models.Model):
    MALE = 1
    FEMALE = 2
    GENDER_CHOICES = ((MALE, u'男'), (FEMALE, u'女'))    
    
    SINGLE = 1
    INLOVE = 2
    MARRIED = 3
    RELATIONSHIP_CHOICES =  ((SINGLE, u'单身'), (INLOVE, u'恋爱'), (MARRIED,u'已婚'))    
        
    user = models.OneToOneField(User, unique=True, verbose_name=u'用户')
    
    gender = models.IntegerField(choices=GENDER_CHOICES, default=FEMALE, verbose_name=u"性别")
    relationship_status = models.IntegerField(choices=RELATIONSHIP_CHOICES, default=SINGLE, verbose_name=u"情感状态")    
    avatar = models.ForeignKey(Image, null=True)
    
    # 生日
    birthday_year = models.IntegerField(default=0)
    birthday_month = models.IntegerField(default=0)
    birthday_day = models.IntegerField(default=0)  
    
    introduction = models.CharField(max_length=256, blank=True, verbose_name=u"签名档")
    country = models.CharField(max_length=64, blank=True, verbose_name=u"国家")  
    location = models.CharField(max_length=64, blank=True, verbose_name=u"现居地")         
    
    created_time = models.DateTimeField(auto_now_add=True)
    updated_time = models.DateTimeField(auto_now=True)
    
    objects = UserProfileManager()    
    
    class Meta:
        verbose_name = u'用户资料'
        verbose_name_plural = u'用户资料列表'
        db_table = "t_user_profile"
        ordering = ["-created_time"]
    
    class Admin:
        pass
    
    def __unicode__(self): #todo 
        return u"%s 的资料" % self.user.username
        
    
    def get_avatar_url(self):
        #import pdb;pdb.set_trace()
        if self.avatar:
            return self.avatar.get_url()
        else:
            return os.path.join(settings.MEDIA_URL, "image", "default_avatar.png")


class UserStats(models.Model):
    """用户统计信息"""
    user = models.OneToOneField(User, unique=True, verbose_name=u'用户')
    total_vote_num = models.IntegerField(default=0)
    


from django.db.models.signals import post_save

def user_save(**kargs):
    sender = kargs.get("sender",None)
    user = kargs.get("instance", None)
    created = kargs.get("created", None)
    if sender == None or sender != User or not user or not created:
        return    
    from account import add_user_other_properties
    add_user_other_properties(user)

post_save.connect(user_save)